// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   FirstLoginController.java

package com.nervytech.tutorcruise.spring.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nervytech.tutorcruise.db.enums.UserTypeEnum;
import com.nervytech.tutorcruise.db.service.interfaces.ICategoryService;
import com.nervytech.tutorcruise.db.service.interfaces.ICityService;
import com.nervytech.tutorcruise.db.service.interfaces.ICompanyProfileService;
import com.nervytech.tutorcruise.db.service.interfaces.IInstituteProfileService;
import com.nervytech.tutorcruise.db.service.interfaces.IStateService;
import com.nervytech.tutorcruise.db.service.interfaces.ITrainerProfileService;
import com.nervytech.tutorcruise.db.service.interfaces.IUserService;
import com.nervytech.tutorcruise.service.exception.TrainerCruiseServiceException;
import com.nervytech.tutorcruise.spring.common.form.FirstLoginForm;
import com.nervytech.tutorcruise.spring.security.auth.SessionUser;
import com.nervytech.tutorcruise.spring.security.auth.UserDetailsServiceImpl;

@Controller
@RequestMapping("/newusr/firstlogin")
public class FirstLoginController {

	@Autowired
	private IUserService userService;
	
	@Autowired
	private ICategoryService catService;
	
	@Autowired
	private IStateService stateService;
	
	@Autowired
	private ICityService cityService;
	
	@Autowired
	private IInstituteProfileService instituteProfileService;
	
	@Autowired
	private ICompanyProfileService companyProfileService;
	
	@Autowired
	private ITrainerProfileService trainerProfileService;
	
	private Map<Integer,String> userTypesMap;
	private Map<Integer,String> categoryMap;
	private Map<Integer,String> statesMap;
	private Map<Integer,String> citiesMap;

	@ModelAttribute("userTypesMap")
	public Map<Integer,String> populateUserTypesMap() {
		if (userTypesMap == null) {
			userTypesMap = new LinkedHashMap<Integer,String>();
			userTypesMap.put(Integer.valueOf(-1), "Select Account Type");
			userTypesMap.put(Integer.valueOf(1), "Trainee");
			userTypesMap.put(Integer.valueOf(2), "Trainer");
			userTypesMap.put(Integer.valueOf(3), "Training Institute");
			userTypesMap.put(Integer.valueOf(4), "Company");
		}
		return userTypesMap;
	}

	@ModelAttribute("categoryMap")
	public Map<Integer,String> populateCategoryMap() {
		if (categoryMap == null) {
			SessionUser userDetails = UserDetailsServiceImpl
					.currentUserDetails();
			long userId = userDetails.getUserId();
			categoryMap = new LinkedHashMap<Integer,String>();
			categoryMap.put(Integer.valueOf(-1), "Select Category");
			Map catsMap;
			try {
				catsMap = catService.getCategoryMap();
			} catch (TrainerCruiseServiceException e) {
				e.printStackTrace();
				catsMap = new LinkedHashMap<Integer,String>();
			}
			System.out
					.println((new StringBuilder("CATSMAPPPPPPPPPPPPPPPPPPPP "))
							.append(catsMap).toString());
			int catId;
			String catName;
			for (Iterator iterator = catsMap.entrySet().iterator(); iterator
					.hasNext(); categoryMap
					.put(Integer.valueOf(catId), catName)) {
				java.util.Map.Entry entry = (java.util.Map.Entry) iterator
						.next();
				catId = ((Integer) entry.getKey()).intValue();
				catName = (String) entry.getValue();
			}

		}
		return categoryMap;
	}

	@ModelAttribute("statesMap")
	public Map<Integer,String> populateStatesMap() {
		if (statesMap == null) {
			SessionUser userDetails = UserDetailsServiceImpl
					.currentUserDetails();
			long userId = userDetails.getUserId();
			statesMap = new LinkedHashMap<Integer,String>();
			statesMap.put(Integer.valueOf(-1), "Select States");
			Map stMap;
			try {
				stMap = stateService.getStatesMap(101);
			} catch (TrainerCruiseServiceException e) {
				e.printStackTrace();
				stMap = new LinkedHashMap<Integer,String>();
			}
			System.out
					.println((new StringBuilder("statesMapPPPPPPPPPPPPPPPP "))
							.append(stMap).toString());
			int catId;
			String catName;
			for (Iterator iterator = stMap.entrySet().iterator(); iterator
					.hasNext(); statesMap.put(Integer.valueOf(catId), catName)) {
				java.util.Map.Entry entry = (java.util.Map.Entry) iterator
						.next();
				catId = ((Integer) entry.getKey()).intValue();
				catName = (String) entry.getValue();
			}

		}
		return statesMap;
	}

	@ModelAttribute("citiesMap")
	public Map<Integer,String> populateCitiesMap() {
		if (citiesMap == null) {
			SessionUser userDetails = UserDetailsServiceImpl
					.currentUserDetails();
			long userId = userDetails.getUserId();
			citiesMap = new LinkedHashMap<Integer,String>();
			citiesMap.put(Integer.valueOf(-1), "Select Cities");
			Map stMap;
			try {
				stMap = cityService.getCitisMap(2);
			} catch (TrainerCruiseServiceException e) {
				e.printStackTrace();
				System.out
						.println("THROWNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");
				stMap = new LinkedHashMap<Integer,String>();
			}
			System.out
					.println((new StringBuilder("statesMapPPPPPPPPPPPPPPPP "))
							.append(stMap).toString());
			int catId;
			String catName;
			for (Iterator iterator = stMap.entrySet().iterator(); iterator
					.hasNext(); statesMap.put(Integer.valueOf(catId), catName)) {
				java.util.Map.Entry entry = (java.util.Map.Entry) iterator
						.next();
				catId = ((Integer) entry.getKey()).intValue();
				catName = (String) entry.getValue();
			}

		}
		return statesMap;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String showFirstLogin(Map model) {
		System.out.println("SHOWFOR FIRSTLOGINNNNNNNNNNNNNNNNNNNNNNNNN");
		FirstLoginForm form = new FirstLoginForm();
		model.put("firstLoginForm", form);
		return "firstlogin";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String saveFirstLogin(FirstLoginForm usrForm, BindingResult result,
			Map model, HttpServletRequest request) {
		SessionUser user = UserDetailsServiceImpl.currentUserDetails();
		UserTypeEnum userType = UserTypeEnum.forValue(usrForm.getUserType());
		System.out.println((new StringBuilder(
				"User Type is ====>>>>>>>>>>>>>>>>>>>>>> ")).append(
				usrForm.getUserType()).toString());
		int mobileVerfied = 1;
		try {
			if (usrForm.getUserType() == UserTypeEnum.TRAINEE.getType())
				userService.updateUserTypeAndCategory(user.getUserId(),
						usrForm.getUserType(), usrForm.getCategory());
			else if (usrForm.getUserType() == UserTypeEnum.TRAINER.getType()) {
				userService.updateFirstLogin(user.getUserId(),
						usrForm.getUserType(), usrForm.getCategory(),
						usrForm.getMobile(), mobileVerfied);
				trainerProfileService.addNewTrainer(user.getUserId(),
						new Date(), new Date());
			} else if (usrForm.getUserType() == UserTypeEnum.INSTITUTE
					.getType()) {
				userService.updateFirstLogin(user.getUserId(),
						usrForm.getUserType(), usrForm.getCategory(),
						usrForm.getMobile(), mobileVerfied);
				instituteProfileService.updateFirstLogin(user.getUserId(),
						usrForm.getPhone(), usrForm.getWebsite(),
						usrForm.getAddress());
			} else if (usrForm.getUserType() == UserTypeEnum.COMPANY.getType()) {
				userService.updateFirstLogin(user.getUserId(),
						usrForm.getUserType(), usrForm.getCategory(),
						usrForm.getMobile(), mobileVerfied);
				companyProfileService.updateFirstLogin(user.getUserId(),
						usrForm.getPhone(), usrForm.getWebsite(),
						usrForm.getAddress(), usrForm.getContactPersonName(),
						usrForm.getContactPersonEmail());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		updateUserTypeInSpringSession(user, userType, request);
		return "redirect:/usr/profile";
	}

	
	private boolean updateUserTypeInSpringSession(SessionUser usr,
			UserTypeEnum userType, HttpServletRequest request) {
		try {
			Collection authorities = new ArrayList();
			authorities.add(new SimpleGrantedAuthority(userType.toString()));
			SessionUser springUser = new SessionUser(usr.getUsername(),
					usr.getPassword(), authorities, true, usr.getFullName(),
					usr.getUserId());
			org.springframework.security.core.Authentication authentication = new UsernamePasswordAuthenticationToken(
					springUser, springUser.getPassword(),
					springUser.getAuthorities());
			SecurityContextHolder.getContext()
					.setAuthentication(authentication);
			request.getSession().setAttribute("SPRING_SECURITY_CONTEXT",
					SecurityContextHolder.getContext());
		} catch (Exception e) {
			e.printStackTrace();
			SecurityContextHolder.getContext().setAuthentication(null);
			return false;
		}
		return true;
	}

}
